package com.hcrazy.manchester.app.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.hcrazy.manchester.app.pojo.Admin;

public class AuthInterceptor extends HandlerInterceptorAdapter{
	
	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
		
		String context = req.getContextPath();
		String url = StringUtils.replace(req.getRequestURI(), context, "");
		if(url.equals("/admin/login")){
			return true;
		}
		
		Admin admin =  null == req.getSession().getAttribute("admin")?null:(Admin)req.getSession().getAttribute("admin");
		if(null != admin && null != admin.getAdminName() && !"".equals(admin.getAdminName())){
			return true;
		}
		req.getRequestDispatcher("/admin/login?url="+url).forward(req, resp);
		
		return false;
	}
	
	@Override
	public void postHandle(HttpServletRequest req, HttpServletResponse resp, Object handler, ModelAndView modelAndView) throws Exception {
		
	}

}
